<template>

  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.close"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form ref="formBillGroupRef" :rules="rulesBillGroup" :model="formBillGroup" label-width="180px">

      <el-form-item label="组名称" prop="name" >
        <el-input v-model.trim="formBillGroup.name"></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="content" >
        <el-input v-model.trim="formBillGroup.content" type="textarea"></el-input>
      </el-form-item>

      <el-form-item label="重要程度" prop="importance" >
        <el-select v-model="formBillGroup.importance" placeholder="请选择重要程度" >
          <el-option label="核心" :value="1"></el-option>
          <el-option label="重要" :value="2"></el-option>
          <el-option label="一般" :value="5"></el-option>
        </el-select>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd" v-if="defaultValue.saveUpdSelect !== 'select'">{{defaultValue.title}}</el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>

</template>

<script>

  import http from '@/utils/http'
  import {clearInvalid} from "../../utils/utils"

  export default {
    name: "BillingGroupSaveUpd",
    data(){
      return {
        // 表格的默认值
        tableConfig: {
          // 关闭时销毁 Dialog 中的元素
          destroyOnClose: true,
          // 弹出框是否打开
          show: false,
          // 是否显示关闭按钮
          close: true,
          // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
          lockScroll: false,
          appendToBody: true,
        },
        // 默认值，进入页面后的值
        defaultValue: {
          // 弹出框标题
          title: '查看',
          // 保存还是修改or查看,取值有 select | save | upd
          saveUpdSelect: 'select'
        },
        // 提交的数据
        formBillGroup:{
          userId: null,
          name: null,
          content: null,
          importance: null,
        },
        // 校验规则
        rulesBillGroup: {
          name: [{ required: true, message: '请输入账单组名称', trigger: 'blur' }],
          content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        },
      }
    },
    methods: {
      /**
       * 初始化数据
       */
      init(){
        this.formBillGroup = {
          userId: null,
          name: null,
          content: null,
          importance: null,
        }
      },
      /**
       * 打开弹出框之前
       * @param type 保存还是修改or查看,取值有 select \ save \ upd
       * @param obj 如果是修改和查看，则是详细数据,需要赋值
       */
      openBillGroup(type, obj){
        this.defaultValue.saveUpdSelect = type
        this.init()

        if (type === 'save'){
          this.defaultValue.title = '新增账单组'

        } else if (type === 'upd'){
          this.defaultValue.title = '修改账单组'
          this.formBillGroup = JSON.parse(JSON.stringify(obj))

        } else {
          this.defaultValue.title = '账单组详情'
          this.formBillGroup = obj
        }

        this.tableConfig.show = true
        this.$store.commit('navigationListChange', ['开支管理', '账单组', this.defaultValue.title])
      },
      /**
       * 是保存还是修改
       */
      toSaveUpd(){
        if (this.defaultValue.saveUpdSelect === 'save'){
          this.saveBillingGroup()

        } else if (this.defaultValue.saveUpdSelect === 'upd'){
          this.updBillingGroup()

        } else {

        }
      },
      /**
       * 更新账单组
       */
      updBillingGroup(){
        clearInvalid(this.formBillGroup)
        http.post("/bill/updBillingGroup", this.formBillGroup).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("更新成功");
          this.$emit('getBillingGroupPageList', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 保存账单组
       */
      saveBillingGroup(){
        http.post("/bill/saveBillingGroup", this.formBillGroup).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("保存成功");
          this.$emit('getBillingGroupPageList', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
    },
  }
</script>

<style scoped>

</style>
